//* Copyright(C) 2005-2012, all rights reserved.
//*
//* The following code is copyrighted and contains proprietary information
/* and trade secrets of Foxit Software Inc.      
 **************************************************************************
 */

/**
 * Thmubnail插件类；
 * 需求的plugin: navigation panel;
 */
define(function (require, exports, module) {
    var PDFPageThumbsView = require("./PDFThumbsView");
    /**
     *  Plugin of thumbnails.
     * @param {WebPDF.ReaderApp} readerApp app instance
     * @extends {WebPDF.IReaderPlugin}
     * @constructor
     */
    WebPDF.ThumbNailPlugin = function (readerApp) {
        var _readerApp = readerApp,
            _thumbsView = null,
            _self = this;

        //下面实现 INavigationChildPanel接口;
        this.getPanelID = function () {
            return _readerApp.getAppId() + "_fxThumbView";
        };
        this.getTabBtnID = function () {
            return _readerApp.getAppId() + "_thumbtab";
        };
        this.getTabNorClass = function () {
            return "fwr-thumbnail-panel-icon";
        };
        this.getTabHotClass = function () {
            return "fwr-thumbnail-hot-panel-icon";
        };
        this.getTitle = function () {
            return "ThumbNail.Title";
        };
        this.onSize = function (panelWidth, panelHeight) {
            $("#" + _self.getPanelID()).css({ width: panelWidth, height: panelHeight });
            if (_thumbsView) {
                _thumbsView.onSize(panelWidth, panelHeight);
            }
        };
        //end INavigationChildPanel接口;

        //获取Plugin名称;
        this.getPluginName = function () {
            return "ThumbNail Plugin";
        };
        //插件被注册的时候调用;
        this.onRegister = function () {
        };
        //模块初始化;
        this.init = function () {
            var docView = _readerApp.getMainView().getDocView();
            _thumbsView = new PDFPageThumbsView(docView.getPDFDoc(), _self.getPanelID(), _readerApp);
            _thumbsView.init();
            _bindEvents();
            _thumbsView.setThumbFocus(docView.getCurPageIndex());
        };
        //模块卸载;
        this.unLoad = function () {
        };

        this.getReaderApp = function () {
            return _readerApp;
        };

        function _bindEvents() {
            $("#" + _self.getPanelID()).on('mousedown', '.thumbimage', function (event) {
                if (event.button == 0 || event.button == 1) {
                    try {
                        if (WebPDF.Environment.mobile) {
                            $.mobile.changePage('#readerPage', {transition:'slide'});
                        }
                        var pageIndex = parseInt(event.target.getAttribute("page-index"));
                        _readerApp.getMainView().getDocView().goToPage(pageIndex);
                        _thumbsView.setSelectedPage(pageIndex);
                    }
                    catch (ex) {
                        console.error(ex);
                        return false;
                    }
                }
            });

            $(_readerApp).on(WebPDF.EventList.DOCVIEW_PAGE_CHANGED,function(event, data){
                _thumbsView.setThumbFocus(data.curIndex);
        });
        }
    };

    exports.createPlugin = function (readerApp) {
        return new WebPDF.ThumbNailPlugin(readerApp);
    };
});